//
//  LJHHistoryViewController.m
//  HEARME
//
//  Created by mac on 16/2/24.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LJHHistoryViewController.h"
#import "LJHFindTableViewCell.h"
@interface LJHHistoryViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)LJHBaseTableView *historyTableView;
@property(nonatomic,retain)NSMutableArray *historyArray;

@end

@implementation LJHHistoryViewController

-(void)dealloc
{
    [_historyTableView release];
    [_historyArray release];
    [super dealloc];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.historyArray = [NSMutableArray arrayWithArray:[[LJHFMData shareFMDB] selectTrackData]];
    [self.historyTableView reloadData];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTableView];
        
    // Do any additional setup after loading the view.
}
-(void)createTableView
{
    
    self.historyTableView = [[LJHBaseTableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStyleGrouped];
    [self.view addSubview:self.historyTableView];
    [_historyTableView release];
    self.historyTableView.dataSource = self;
    self.historyTableView.delegate = self;
    [self.historyTableView registerClass:[LJHFindTableViewCell class] forCellReuseIdentifier:@"historyTableCell"];
    [self.historyTableView reloadData];
    self.historyTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
}

#pragma mark 设置tableview区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.historyArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LJHFindTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"historyTableCell"];
    
    LJHChildListModel *model = [self.historyArray objectAtIndex:indexPath.row];
    
    cell.model = model;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return WIDTH*0.24;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return HEIGHT*0.06;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    LJHChildListModel *model = self.historyArray[indexPath.row];
    LJHPlayerViewController *player = [LJHPlayerViewController sharaMusicPlayer];
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:player];
    player.song_id = model.trackId;
    [self presentViewController:naVC animated:YES completion:^{
        
    }];
    
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    LJHBaseButton *headBtn = [LJHBaseButton buttonWithType:UIButtonTypeCustom];
    headBtn.frame = CGRectMake(0, 0, WIDTH, HEIGHT*0.06);
    [headBtn setTitle:@"清空历史记录" forState:UIControlStateNormal];
    [headBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [headBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    return headBtn;
}
-(void)btnAction:(UIButton *)btn
{
    [[LJHFMData shareFMDB] dropTableTracks];
    self.historyArray = [NSMutableArray array];
    [self.historyTableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
